package com.wireless.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.wireless.base.BaseAction;
import com.wireless.entity.Menutbl;
import com.wireless.entity.Menutypetbl;
import com.wireless.entity.Orderdetailtbl;
import com.wireless.entity.Ordertbl;
import com.wireless.entity.Report;
import com.wireless.entity.Tabletbl;
import com.wireless.service.MenuService;
import com.wireless.service.OrderService;
import com.wireless.service.TableService;
import com.wireless.util.CreateOrderId;

@Namespace("/order")
@ParentPackage("json-default")
@Controller
public class OrderAction extends BaseAction {
	@Autowired
	private OrderService orderService;
	@Autowired
	private TableService tableService;
	@Autowired
	private MenuService menuService;
	private Ordertbl order;
	private List<Ordertbl> orderlist;
	private int oid;
	private String menuIds;
	private String counts;
	private String tableId;
	List<Object[]> objects;
	List<Report> reportList = new ArrayList<Report>();

	@Action(value = "orderlist", results = { @Result(name = "success", location = "/views/order/order.jsp") })
	public String orderlist() {
		orderlist = orderService.getOrderList();
		return SUCCESS;
	}

	@Action(value = "deleteOrder", results = { @Result(name = "success", location = "/views/order/order.jsp") })
	public String deleteOrder() {
		orderService.delOrder(oid);
		orderlist = orderService.getOrderList();
		return SUCCESS;
	}

	@Action(value = "orderDetail", results = { @Result(name = "success", location = "/views/order/orderPre.jsp") })
	public String orderDetail() {
		order = orderService.getOrder(oid);
		return SUCCESS;
	}

	@Action(value = "findReportByMouth", results = { @Result(name = "success", type = "json") })
	public String findReportByMouth() {
		objects = orderService.findReportByMouth("");
		for (Object[] object : objects) {
			Report r = new Report();
			r.setValue(object[0].toString());
			r.setName(object[1].toString());
			reportList.add(r);
		}
		return SUCCESS;
	}

	@Action(value = "findReportByYear", results = { @Result(name = "success", type = "json") })
	public String findReportByYear() {
		objects = orderService.findReportByYear("");
		for (Object[] object : objects) {
			Report r = new Report();
			r.setValue(object[0].toString());
			r.setName(object[1].toString());
			reportList.add(r);
		}
		return SUCCESS;
	}

	@Action(value = "findReportByDay", results = { @Result(name = "success", type = "json") })
	public String findReportByDay() {
		objects = orderService.findReportByDay("");
		for (Object[] object : objects) {
			Report r = new Report();
			r.setValue(object[0].toString());
			r.setName(object[1].toString());
			reportList.add(r);
		}
		return SUCCESS;
	}

	@Action(value = "changeOrderPayFlag", results = { @Result(name = "success", type = "json") })
	public String changeOrderPayFlag() {
		orderService.changeOrderPayFlag(oid);
		return SUCCESS;
	}

	@Action(value = "addOrder", results = { @Result(name = "success", type = "json") })
	public String addOrder() {
		Ordertbl order = new Ordertbl();
		Set<Orderdetailtbl> list = new HashSet<Orderdetailtbl>();
		String[] MenuIds = menuIds.split(",");
		String[] Counts = counts.split(",");
		Orderdetailtbl orderdetail = null;
		// 添加订单主表信息
		Tabletbl tabletbl = tableService.getATable(Integer.valueOf(tableId));
		order.setTabletbl(tabletbl);// 桌号
		order.setId(Integer.valueOf(CreateOrderId.getOrderId()));// 使用订单号
		tabletbl.setOrdId(order.getId());// 设置桌子的订单号
		tabletbl.setFlag("0");// 设置桌子的状态
		order.setOrderTime(new Timestamp(System.currentTimeMillis()));// 订单时间
		order.setIdPay(0);// 支付状态
		for (int i = 0; i < MenuIds.length; i++) {
			System.out
					.println(MenuIds[i].toString()
							+ "————————————————————————————————————————————————————————————");
			orderdetail = new Orderdetailtbl();
			Menutbl menutbl = menuService.getMenu(Integer.valueOf(MenuIds[i]
					.toString()));
			orderdetail.setMenutbl(menutbl);
			orderdetail.setNum(Integer.valueOf(Counts[i].toString()));
			orderdetail.setOrdertbl(order);
			list.add(orderdetail);
		}
		order.setOrderdetailtbls(list);
		tableService.updTable(tabletbl);
		orderService.addOrder(order);
		return SUCCESS;
	}

	public Ordertbl getOrder() {
		return order;
	}

	public void setOrder(Ordertbl order) {
		this.order = order;
	}

	public List<Ordertbl> getOrderlist() {
		return orderlist;
	}

	public void setOrderlist(List<Ordertbl> orderlist) {
		this.orderlist = orderlist;
	}

	public int getOid() {
		return oid;
	}

	public void setOid(int oid) {
		this.oid = oid;
	}

	public String getMenuIds() {
		return menuIds;
	}

	public void setMenuIds(String menuIds) {
		this.menuIds = menuIds;
	}

	public String getCounts() {
		return counts;
	}

	public void setCounts(String counts) {
		this.counts = counts;
	}

	public String getTableId() {
		return tableId;
	}

	public void setTableId(String tableId) {
		this.tableId = tableId;
	}

	public List<Report> getReportList() {
		return reportList;
	}

	public void setReportList(List<Report> reportList) {
		this.reportList = reportList;
	}

}
